package com.jwdf.uda.ui.fragment.user.personal;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentCoverBinding;
import com.jwdf.uda.event.CoverEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.GetCoverAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;
import com.jwdf.uda.view.GridCoverLayout;

import java.io.File;
import java.util.List;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/8.
 */
public class CoverFragment extends BaseFragment {

    private FragmentCoverBinding mBinding;
    private String mCoverAvatar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mBinding = FragmentCoverBinding.inflate(inflater, container, false);
        initViews();
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        showLoadingPage(R.id.rl_loading);
    }

    private void initViews(){
        requestCoverList();
    }
    private void requestCoverList() {
        onNewRequestCall(GetCoverAction.newInstance(getContext()).request(new AHttpService.IResCallback<PbBean.PbResSystemGetCoverList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResSystemGetCoverList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {

                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_operation))) {
                        return;
                    }
                    PbBean.PbResSystemGetCoverList body = response.body();
                    if (body != null) {
                        LoadCoverList(body);
                    }
                }
            }
        }));
    }

    private  void LoadCoverList(PbBean.PbResSystemGetCoverList body){
        final List<PbBean.PbSystemCover> images = body.getCoverListList();
        mBinding.gridCoverLayout.setImage(images);
        closeLoadingPage();
        mBinding.gridCoverLayout.setOnImageItemClickListener(new GridCoverLayout.OnImageItemClickListener() {
            @Override
            public void onImageClick(int index, LinearLayout linearLayout) {
                downloadImage(images.get(index).getCover().getUrl());
            }
        });
    }


    private void downloadImage(String url) {
        showProgressDialog();
        String dir = Environment.getExternalStorageDirectory() + File.separator + "Uda/Image";
        UdaUtils.downloadImage(getContext(), url, dir, new UdaUtils.ImageLoadingListener() {
            @Override
            public void onComplete(String path) {
                closeProgressDialog();
                CoverEvent coverEvent=new CoverEvent(CoverEvent.EVENT_COVER);
                coverEvent.cover=path;
                EventBusUtil.post(coverEvent);
            }

            @Override
            public void onError() {
                closeProgressDialog();
                mCoverAvatar = null;
                ToastUtils.show(getContext(),"网络不给力");
            }
        });
    }
}
